﻿using System;
using System.Collections.Generic;
using Assist;
using YJKSparseDistance.ModelLayer;
using YJKSparseDisOfRoom.ModelLayer;
using YJKSparseDisOfRoom.Properties;
using YJKGeometryFuncs;

namespace YJKSparseDisOfRoom.ControlLayer
{
	internal class BaseInRoomCalculater
	{
		public void CalculateRoomNodePacker(RoomNodePacker inputPacker)
		{
			inputPacker.PrePareAllEndPoints();
			List<ResultBean_InRoom> list = new List<ResultBean_InRoom>();
			WayMapBean wayMapBean = null;
			foreach (EndPointPacker_InRoom startPoint in inputPacker.LstStartPoints)
			{
				foreach (KeyValuePair<EndPointPacker_InRoom, DoorLinkNode> keyValuePair in inputPacker.LstKVPEndPoint)
				{
					ResultBean_InRoom resultBean_InRoom;
					if (!this.QuickFilteCalculate(startPoint, keyValuePair.Key, inputPacker) && this.TryCalculateOneWay(startPoint, keyValuePair.Key, inputPacker, ref wayMapBean, out resultBean_InRoom))
					{
						if (keyValuePair.Value == null || keyValuePair.Value.LstThisDoorResults.Count == 0)
						{
							list.Add(resultBean_InRoom);
						}
						else
						{
							foreach (ResultBean_InRoom inputBean in keyValuePair.Value.LstThisDoorResults)
							{
								list.Add(resultBean_InRoom.AppendPoints(inputBean));
							}
						}
					}
				}
			}
			inputPacker.CalculateConditionEnum = NodeConditionEnum.FinishCalculate;
			inputPacker.LstAllResults = list;
			inputPacker.MakeAppendBeanInThisRoom();
		}

		private bool QuickFilteCalculate(EndPointPacker_InRoom startPoint, EndPointPacker_InRoom endPoint, RoomNodePacker inputRoomPacker)
		{
			return !(startPoint.UseElementPacker is DoorPacker_EvacuationDistance) && Geometry.Lessthan_Or_Equal(startPoint.UseEndPoint.DistanceTo(endPoint.UseEndPoint), BaseInRoomCalculater.m_dQuickFilterDistanceValue);
		}

		private bool TryCalculateOneWay(EndPointPacker_InRoom startPoint, EndPointPacker_InRoom endPoint, RoomNodePacker inputRoomPacker, ref WayMapBean useWayMap, out ResultBean_InRoom result)
		{
			if (useWayMap == null)
			{
				useWayMap = new WayMapBean(inputRoomPacker);
			}
			useWayMap.SetStartPoint(startPoint);
			useWayMap.SetEndPoint(endPoint);
			return useWayMap.TryCalculate(out result);
		}

		private static double m_dQuickFilterDistanceValue = AssistFunc.mmToFeet(double.Parse(Resources.str_QuickFilterDistanceValue) * 1000.0);
	}
}
